<?php
/**
* @filename course_note.php 2016-9-6 02:10:56x
* @author Yunfent Zhang <306232452@qq.com>
* @version 1.0.0
* @copyright DZF (c) 2016, Yunfeng Zhang
*/
header("Content-Type: text/html;charset=utf-8");
class ctrl_course_hour_record extends course_hour_record {
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
	
	static function &instance() {
		static $object;
		if (empty($object)) {
			$object = new self();
		}
		return $object;
	}
	/* public function do_continue_detail() {		
		global $_G;			
		
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$cm_id = isset($_REQUEST['cm_id']) && !empty($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
		$user_id = isset($_G['user_id']) && !empty($_G['user_id']) ? $_G['user_id'] : '';
		$result_data=array();
		if($cm_id&&$user_id){			
			$result_data = $this->get_continue_detail($cm_id,$user_id);
		}		
		//DEBUG 初始化请求请求获取一页列表数据的参数
				
		//DEBUG 获取各类信息分类名称 结束		
		//DEBUG 返回输出
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';		
		if($result_data){		
			$return['code'] = '0';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}		 
			
	} */
	public function do_continue_detail() {
		global $_G;
	
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$cm_id = isset($_REQUEST['cm_id']) && !empty($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
		$user_id = isset($_G['user_id']) && !empty($_G['user_id']) ? $_G['user_id'] : '';
		$result_data=array();
		if($cm_id&&$user_id){
			$result_data = $this->get_continue_detail($cm_id,$user_id);
		}
		//DEBUG 初始化请求请求获取一页列表数据的参数
	
		//DEBUG 获取各类信息分类名称 结束
		//DEBUG 返回输出
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
		if($result_data){
			$return['code'] = '0';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
			
	}
			
	/* public function do_click_course_hour() {
		global $_G;
		
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$cm_id = isset($_REQUEST['cm_id']) && !empty($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
		$user_id = isset($_G['user_id']) && !empty($_G['user_id']) ? $_G['user_id'] : '';
		$result_data=array();
		
		if($cm_id&&$user_id&&$ch_id){			
			$result_data = $this->get_click_hour_detail($cm_id,$ch_id,$user_id);
		}
		//DEBUG 初始化请求请求获取一页列表数据的参数
	
		//DEBUG 获取各类信息分类名称 结束
		//DEBUG 返回输出
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
		if($result_data){
			$return['code'] = '0';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
			
	} */
	public function do_click_course_hour() {
		global $_G;
	
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$cm_id = isset($_REQUEST['cm_id']) && !empty($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
		$user_id = isset($_G['user_id']) && !empty($_G['user_id']) ? $_G['user_id'] : '';
		$result_data=array();
	
		if($cm_id&&$user_id&&$ch_id){
			$result_data = $this->get_click_hour_detail($cm_id,$ch_id,$user_id);
		}
		//DEBUG 初始化请求请求获取一页列表数据的参数
	
		//DEBUG 获取各类信息分类名称 结束
		//DEBUG 返回输出
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
		if($result_data){
			$return['code'] = '0';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=$result_data;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
			
	}
	//进入课堂相关
	public function do_enter_course() {
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';		
		$cm_id = isset($_REQUEST['cm_id']) && !empty($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		
		$res_data=array(
			"cm_id"=>$cm_id,
			"ch_id"=>$ch_id,				
			"user_id"  => $_G['user_id'],
			"time"  => TIMESTAMP
		);	
		$where = " and ch.isdelete = 0  and ch.cm_id = ".$cm_id." and chr.user_id = ".$_G['user_id'];
		if($ch_id ){
			$where .=" and ch.ch_id =  ".$ch_id;
		}
		$enter_info= $this->enter_info('course_hour',$res_data,$where);
		if($enter_info){
			$return['code'] = '0';
			$return['data']=$enter_info;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
		
	
			
	}
	
	/*
	 *  添加课时累计时间
	 */
	/* public function do_add_time() {
		global $_G;	
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
		$add_time = isset($_REQUEST['add_time']) && !empty($_REQUEST['add_time']) ? $_REQUEST['add_time'] : '';
		
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		if($ch_id){
			$res_data=array(
		    	"ch_id"=>$ch_id,
				"add_time"=>$add_time,
				"user_id" =>$_G['user_id'],	
				"modify_dateline"=> strval(TIMESTAMP)
			);

			$where = array('ch_id'=>$ch_id);
			$effect_row = $this->add_time('course_hour_record',$res_data,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$res_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
		
	} */
	public function do_add_time() {
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
		$add_time = isset($_REQUEST['add_time']) && !empty($_REQUEST['add_time']) ? $_REQUEST['add_time'] : '';		
		$key = isset($_REQUEST['key']) && !empty($_REQUEST['key']) ? $_REQUEST['key'] : '';
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		if($ch_id){
			$res_data=array(
					"ch_id"=>$ch_id,
					"add_time"=>$add_time,
					"user_id" =>$_G['user_id'],
					'key'     => $key,
					"modify_dateline"=> strval(TIMESTAMP)
			);
	
			$where = array('ch_id'=>$ch_id,'user_id'=>$_G['user_id']);
			$effect_row = $this->add_time('course_hour_looked',$res_data,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$res_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	}
	
	/*
	 *  设置课时播放时间
	 */
	/*
	 *  设置课时播放时间
	 */
	public function do_set_time() {
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
		$look_time = isset($_REQUEST['look_time']) && !empty($_REQUEST['look_time']) ? $_REQUEST['look_time'] : '';
	
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		if($ch_id){
			$res_data=array(
					"ch_id"=>$ch_id,
					"look_time"=>$look_time,
					"user_id"  => $_G['user_id'],
					"modify_dateline"=> strval(TIMESTAMP),
	
			);
	
			$where = array('ch_id'=>$ch_id,'user_id'=>$_G['user_id']);
			$effect_row = $this->edit_hour_record('course_hour_looked',$res_data,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$res_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	}
	/* public function do_set_time() {
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
		$look_time = isset($_REQUEST['look_time']) && !empty($_REQUEST['look_time']) ? $_REQUEST['look_time'] : '';
	
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		if($ch_id){
			$res_data=array(
					"ch_id"=>$ch_id,
					"look_time"=>$look_time,
					"user_id"  => $_G['user_id'],
					"modify_dateline"=> strval(TIMESTAMP),
										
			);
	
			$where = array('ch_id'=>$ch_id);
			$effect_row = $this->edit_hour_record('course_hour_record',$res_data,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$res_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	} */
	
	/*
	 *  设置课时已经看完
	 */
	/* public function do_is_looked() {
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';		
	
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		if($ch_id){
			$res_data=array(
					"ch_id"=>$ch_id,
					"is_looked"=>1,//1表示已经看完
					"user_id"  => $_G['user_id'],
					"look_time" =>0,
					"modify_dateline"=> strval(TIMESTAMP),
					
			);	
			$where = array('ch_id'=>$ch_id);
			$effect_row = $this->edit_is_looked('course_hour_record',$res_data,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$res_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	} */
	public function do_is_looked() {
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
		$key = isset($_REQUEST['key']) && !empty($_REQUEST['key']) ? $_REQUEST['key'] : '';
	    if(!$ch_id||!$_G['user_id']){
	    	$return['code'] = '1';
	    	$return['data']=array();
	    	echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
	    }
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		if($ch_id){
			$res_data=array(
					"ch_id"=>$ch_id,
					"is_looked"=>1,//1表示已经看完
					"user_id"  => $_G['user_id'],
					"look_time" =>0,
					'key'     => $key,
					"modify_dateline"=> strval(TIMESTAMP),
						
			);
			$where = array('ch_id'=>$ch_id,'user_id'=>$_G['user_id']);
			$effect_row = $this->edit_is_looked('course_hour_looked',$res_data,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$res_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	}
	
	/*
	 *  编辑笔记
	 */
	public function do_edit() {
		global $_G;
		//$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$course_note = isset($_REQUEST['course_note']) && !empty($_REQUEST['course_note']) ? $_REQUEST['course_note']:'';		
		$cn_id = isset($_REQUEST['cn_id']) ? $_REQUEST['cn_id'] : '';
		if($cn_id){			
			$course_note['modify_dateline'] = TIMESTAMP;
			//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
			foreach($course_note AS $key => $value){
				if(is_array($value) && !empty($value)){
					$course_note[$key] = implode(",", $value);
				}
			}
			$where = array('cn_id'=>$cn_id);
			$effect_row = $this->edit('course_note',$course_note,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$course_note;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}			
		}else{
			$return['code'] = '2';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
	
	/*
	 *  删除笔记
	 */
	public function do_delete() {
		global $_G;
		$cn_id = isset($_REQUEST['cn_id']) ? $_REQUEST['cn_id'] : '';
		if($cn_id){
			$effect_row = $this->delete("course_note", array("cn_id" => $cn_id), $limit=1);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}
	}
	
	/*
	 *  判断课时学习是否到时间可以进入
	 */
	public function do_is_start_learn() {	
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$ch_id = isset($_REQUEST['ch_id']) && !empty($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';		
	
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		if($ch_id){
			$res_data=array(
				"ch_id"=>$ch_id,				
				"user_id"  => $_G['user_id'],
				"time"  => TIMESTAMP
			);	
			$where = array('ch_id'=>$ch_id);
			$is_start= $this->is_start('course_hour',$res_data,$where);
			if($is_start){
				$return['code'] = '0';
				$return['data']=$res_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '3';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	}
}
?>